package com.anabur.bean.abstracts;

import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.primefaces.event.FileUploadEvent;
import org.w3c.dom.Node;

import com.anabur.bean.table.AbstractTableItem;
import com.anabur.dao.DAOFactory;
import com.anabur.persistent.Empresa;
import com.anabur.persistent.Rubro;
import com.anabur.system.FileUploadController;

public abstract class AbstractImportBean extends AbstractBean{
	
	private static final long serialVersionUID = 1L;
	private List<Rubro> rubroList;
	private List<AbstractTableItem> uploadedFileList;
	private List<AbstractTableItem> incompleteFileList;
	protected SimpleDateFormat formatDate = new SimpleDateFormat(FORMAT_DATE);
	
	public AbstractImportBean(){
		rubroList = new ArrayList<Rubro>();
		uploadedFileList = new ArrayList<AbstractTableItem>(); 
		incompleteFileList = new ArrayList<AbstractTableItem>();
		reWrite = false; 
	}
	
	public void uploadFile(FileUploadEvent event) {
		File file = FileUploadController.handleFileUpload(event);
		importFile(file);
	}
	
	public void importIncompleteFiles(){
		try{
			List<AbstractTableItem> deleteItems = new ArrayList<AbstractTableItem>();
			List<AbstractTableItem> incompleteFileListTemp = new ArrayList<AbstractTableItem>(incompleteFileList);
			for(AbstractTableItem item : incompleteFileListTemp){
				if(item.getIsSelected()){
					Rubro rubro = (Rubro) DAOFactory.getInstance().getRubroDAO().findPersistentByID(item.getRubroID());
					Empresa empresa = new Empresa(item.getCodigoEmpresa(), item.getNombreEmpresa(), rubro, item.getEsMerval(), item.getEsMerval25());
					DAOFactory.getInstance().getEmpresaDAO().insertOrModify(empresa);
					importFile(item.getFile());
					deleteItems.add(item);
				}
			}
			incompleteFileList.removeAll(deleteItems);
		}catch (Exception e) {
			logErrorInPopupWithoutDetail(e);
		}
	}
	
	public List<AbstractTableItem> getUploadedFileList() {
		return uploadedFileList;
	}

	public void setUploadedFileList(List<AbstractTableItem> uploadedFileList) {
		this.uploadedFileList = uploadedFileList;
	}
	
	public List<AbstractTableItem> getIncompleteFileList() {
		return incompleteFileList;
	}

	public void setIncompleteFileList(List<AbstractTableItem> incompleteFileList) {
		this.incompleteFileList = incompleteFileList;
	}
	
	public abstract void importFile(File file);
	protected abstract void parseHeaderColumn(Node rowNode) throws Exception;
	
	public void cleanView() {
		uploadedFileList = new ArrayList<AbstractTableItem>();
		incompleteFileList = new ArrayList<AbstractTableItem>();
		reWrite = false;
	}
	
	private Boolean reWrite;
	
	public Boolean getReWrite() {
		return reWrite;
	}

	public void setReWrite(Boolean reWrite) {
		this.reWrite = reWrite;
	}
	
	public List<Rubro> getRubroList() {
		return rubroList;
	}

	public void setRubroList(List<Rubro> rubroList) {
		this.rubroList = rubroList;
	}
	
	protected void addIncompleteFile(AbstractTableItem item) {
		try {
			incompleteFileList.add(item);
			rubroList = DAOFactory.getInstance().getRubroDAO().getRubroList();
		} catch (Exception e) {
			logErrorInPopupWithoutDetail(e);
		}
	}
	
	protected void addUploadedFile(AbstractTableItem item) {
		uploadedFileList.add(item);
	}
	
	public void validatePropertyPosition(Map<String, Integer> propertyPositionMap) throws Exception{
		String propertyNotFound = "";
		for(String propertyName : propertyPositionMap.keySet()){
			Integer position = propertyPositionMap.get(propertyName);
			if(position.compareTo(-1) == 0){
				propertyNotFound +=  propertyName + " ";
			}
		}
		if(!propertyNotFound.equals("") & !propertyNotFound.equals("ResultadosExtraodrinarios ")){
			throw new Exception("Las siguientes columnas no se encontraron: " + propertyNotFound);
		}
	}
	
	public boolean getIsBigdecimal(String columnValue) {
		try {
			new BigDecimal(columnValue);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

}
